package com.ylw.dao;

import com.ylw.entity.Order;
import com.ylw.entity.User;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;

/**
 * Created by yangliwei on 2017/7/13.
 */
public class OrderDao {
    SessionFactory sessionFactory = null;
    @Before
    public void before(){
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }


    @Test
    public void save(){
        Order order = new Order();
        User user = new User();
        user.setId(new Long(1));
        order.setUser(user);
        order.setGood("I'm a good");
//        order.setGoodNum(3);
        order.setPayTime(new Date());
        Session session = sessionFactory.openSession();
        session.save(order);
        session.close();


    }

    @Test
    public void test(){

        Order order = new Order();
        Criterion criterion = Restrictions.eq("user.id",1);

        Criteria criteria  = sessionFactory.openSession().createCriteria(Order.class);
        criteria.add(criterion);
        criteria.list();//出现异常 java.lang.Integer cannot be cast to java.lang.Long应该是存数据库的时候自动变成Integer了出来的时候抛异常


    }
}
